<?php
include APPLICATION_PATH."/models/Area_Agent_Product.php";
include APPLICATION_PATH."/models/Agent_Product.php";
include APPLICATION_PATH."/models/Area.php";
include APPLICATION_PATH."/models/Province.php";
include APPLICATION_PATH."/models/Product.php";
include APPLICATION_PATH."/models/District.php";
include APPLICATION_PATH."/models/Agent.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Admin_AreaagentProductController extends Zend_Controller_Action{
    public function  init(){
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->model = new Model_Area_Agent_Product();
        $this->modelMapper = new Model_Area_Agent_ProductMapper();
        $this->AgentProductmodel= new Model_Agent_Product();
        $this->AgentProductmodelMapper= new Model_Agent_ProductMapper();
        $this->Areamodel= new Model_Area();
        $this->AreamodelMapper= new Model_AreaMapper();
        $this->Provincemodel= new Model_Province();
        $this->ProvincemodelMapper= new Model_ProvinceMapper();
        $this->Districtmodel= new Model_District();
        $this->DistrictmodelMapper= new Model_DistrictMapper();
        $this->Productmodel= new Model_Product();
        $this->ProductmodelMapper= new Model_ProductMapper();
        $this->Agentmodel= new Model_Agent();
        $this->AgentmodelMapper= new Model_AgentMapper();
    }
    public function indexAction(){
        
    }
    public function addAction(){
        if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."admin/areaagentproduct/index";  

            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }
            if(isset($_POST["area_id"]))
            {
                $this->model->setArea_Id($_POST["area_id"]);
            } 
            if(isset($_POST["province_id"])){
                $this->model->setProvince_Id($_POST["province_id"]);
            }
            if(isset($_POST["district_id"])){
                $this->model->setDistrict_Id($_POST["district_id"]);
            }
            if(isset($_POST["agent_id"])){
                $this->model->setAgent_Id($_POST["agent_id"]);
            }
            if(isset($_POST["product_id"])){
                $this->model->setProduct_Id($_POST["product_id"]);
            }
            if(isset($_POST["factor"])){
                $this->model->setFactor($_POST["factor"]);
            }
            if(isset($_POST["from_date"])){

                $this->model->setFrom_Date(GlobalLib::toMysqlDateString($_POST["from_date"]));
            }
            if(isset($_POST["to_date"])){
                $this->model->setTo_Date(GlobalLib::toMysqlDateString($_POST["to_date"]));
            }
            if(isset($_POST["order"])){
                $this->model->setOrder($_POST["order"]);
            }
            if(isset($_POST["event"])){
                $event=1;
            } else {
                $event=0;
            }
            if($_POST["product_id"]==0 && $_POST["product_category_id"]>0){
                $products = $this->ProductmodelMapper->getProductByCategoryId($_POST["product_category_id"]);
                
                foreach ($products as $item) {
                    $this->model->setProduct_Id($item->getId());
                    $this->model->setEvent($event);
                    $this->model->setCreated(date("Y/m/d H:i:s"));
                    $this->model->setCreated_By(GlobalLib::getLoginId());
                    $this->model->setModified(date("Y/m/d H:i:s"));
                    $this->model->setModified_By(GlobalLib::getLoginId());
                    $this->model->setIsDeleted(1);
                    $this->modelMapper->save($this->model);
                    //var_dump($item->getId());
                }
                //exit();
                
            }
            else {
                $this->model->setEvent($event);
                $this->model->setCreated(date("Y/m/d H:i:s"));
                $this->model->setCreated_By(GlobalLib::getLoginId());
                $this->model->setModified(date("Y/m/d H:i:s"));
                $this->model->setModified_By(GlobalLib::getLoginId());
                $this->model->setIsDeleted(1);
                $this->modelMapper->save($this->model);
            }
            $this->_redirect($redirectUrl);
        }
        $this->view->item=$this->model;
        $area_id= $this->model->getArea_Id();
        if(empty($area_id)){
            $area_id=0;
        }
        $this->view->areaHTML= GlobalLib::getComboByArea('area_id', 'area', 'id', 'name', 0,false, 'form-control','', '','','');
        $this->view->provinceHTML=  GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', 0, false, 'form-control', '', '', '', '');
        $this->view->districtHTML=  GlobalLib::getComboByDistrict('district_id', 'district', 'id','name', 0,false,'form-control','','','');
        $this->view->agentHTML= GlobalLib::getComboByAgent('agent_id', 'agent', 'id', 'name',0,false,'form-control','','','');
        $this->view->productcategoryHTML= GlobalLib::getComboByProductCategory('product_category_id','product_category','id','name',0,false,'form-control','','');
        $this->view->productHTML= GlobalLib::getComboByProduct('product_id','product','id','name',0,false,'form-control','','');
    }
    public function editAction(){
           $id = $this->_getParam("id","");        
           $this->view->item=$id;
           $redirectUrl=$this->aConfig["site"]["url"]."admin/areaagentproduct/index";
           if(empty($id)){
              $this->_redirect($redirectUrl);
           }
          $this->modelMapper->find($id,$this->model);
          $getId=$this->model->getId();
          if(empty($getId)){
              $this->_redirect($redirectUrl);
          }
          if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."admin/areaagentproduct/index";  

            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }
            if(isset($_POST["area_id"]))
            {
                $this->model->setArea_Id($_POST["area_id"]);
            } 
            if(isset($_POST["agent_id"])){                
                $this->model->setAgent_Id($_POST["agent_id"]);
            }
            if(isset($_POST["factor"])){
                $this->model->setFactor($_POST["factor"]);
            }
            if(isset($_POST["from_date"])){

                $this->model->setFrom_Date(GlobalLib::toMysqlDateString($_POST["from_date"]));
            }
            if(isset($_POST["to_date"])){
                $this->model->setTo_Date(GlobalLib::toMysqlDateString($_POST["to_date"]));
            }
            if(isset($_POST["order"])){
                $this->model->setOrder($_POST["order"]);
            }
            if(isset($_POST["event"])){
                $event=1;
            } else {
                $event=0;
            }
            if($_POST["product_id"]==0 && $_POST["product_category_id"]>0){
                $products = $this->ProductmodelMapper->getProductByCategoryId($_POST["product_category_id"]);
                
                foreach ($products as $item) {
                    $this->model->setProduct_Id($item->getId());
                    $this->model->setEvent($event);
                    $this->model->setCreated(date("Y/m/d H:i:s"));
                    $this->model->setCreated_By(GlobalLib::getLoginId());
                    $this->model->setModified(date("Y/m/d H:i:s"));
                    $this->model->setModified_By(GlobalLib::getLoginId());
                    $this->model->setIsDeleted(1);
                    $this->modelMapper->save($this->model);
                    //var_dump($item->getId());
                }
                //exit();
                
            }
            else {
                $this->model->setEvent($event);
                $this->model->setCreated(date("Y/m/d H:i:s"));
                $this->model->setCreated_By(GlobalLib::getLoginId());
                $this->model->setModified(date("Y/m/d H:i:s"));
                $this->model->setModified_By(GlobalLib::getLoginId());
                $this->modelMapper->save($this->model);
            }
            $this->_redirect($redirectUrl);
        }
        $this->view->item=$this->model;
        $area_id= $this->model->getArea_Id();
        $this->AgentProductmodelMapper->find($this->model->getAgent_Id(),$this->AgentProductmodel);
        if(empty($area_id)){
            $area_id=0;
        }
        $agent_id=$this->AgentProductmodel->getAgent_Id();
        if(empty($agent_id)){
            $agent_id=0;
        }
        $product_id=$this->AgentProductmodel->getProduct_Id();
        if(empty($product_id)){
            $product_id=0;
        }
        
        $this->view->areaHTML= GlobalLib::getComboByArea('area_id', 'area', 'id', 'name', $this->model->getArea_Id(),false, 'form-control','','','','');
        $this->view->provinceHTML=  GlobalLib::getComboByProvince('province_id', 'province', 'id', 'name', $this->model->getProvince_Id(), false, 'form-control', '', '', '', '');
        $this->view->districtHTML=  GlobalLib::getComboByDistrict('district_id', 'district', 'id','name', $this->model->getDistrict_Id(),false,'form-control','','','');
        $this->view->agentHTML= GlobalLib::getComboByAgent('agent_id', 'agent', 'id', 'name',$this->model->getAgent_Id(),false,'form-control','','','');
        $_product = $this->ProductmodelMapper->findbyid($this->model->getProduct_Id());
        
        $this->view->productcategoryHTML= GlobalLib::getComboByProductCategory('product_category_id','product_category','id','name',$_product->getProduct_Cat_Id(),false,'form-control','','');
        $this->view->productHTML= GlobalLib::getComboByProduct('product_id','product','id','name',$this->model->getProduct_Id(),false,'form-control','','');
    }   
    
    public function serviceAction(){
         $this->_helper->layout->disableLayout();
        foreach ($this->modelMapper->fetchAll() as $items ) 
        {      
            if ($items->isdeleted == 1) {
                if ($items->getEvent() == 1)
                    $event = "Có";
                else
                    $event = "Không";
                if ($items->getArea_Id() == 0) {
                    $area_name = "Tất cả";
                } else {
                    $this->AreamodelMapper->find($items->getArea_Id(),$this->Areamodel);
                    $areaId=$this->Areamodel->getId();
                    if(empty($areaId)){
                        $area_name = "Khu vực này đã bị xoá";
                    } else {
                        $area_name = (GlobalLib::getName('area', $items->getArea_Id(), 'name'));
                    }
                }
                if ($items->getProvince_Id() == 0) {
                    $province_name = "Tất cả";
                } else {
                    $this->ProvincemodelMapper->find($items->getProvince_Id(),$this->Provincemodel);
                    $provinceid=$this->Provincemodel->getId();
                    if(empty($provinceid)){
                        $province_name = "Tỉnh thành này đã bị xoá";
                    } else {
                        $province_name = (GlobalLib::getName('province', $items->getProvince_Id(), 'name'));
                    }
                }
                if ($items->getDistrict_Id() == 0) {
                    $district_name = "Tất cả";
                } else {
                    $this->DistrictmodelMapper->find($items->getDistrict_Id(),$this->Districtmodel);
                    $districtid=$this->Districtmodel->getId();
                    if(empty($districtid)){
                        $district_name = "Huyện này đã bị xoá";
                    } else {
                        $district_name = (GlobalLib::getName('district', $items->getDistrict_Id(), 'name'));
                    }
                }
                if ($items->getAgent_Id() == 0) {
                    $agent_name = "Tất cả";
                } else {
                    $this->AgentmodelMapper->find($items->getAgent_Id(),$this->Agentmodel);
                    //var_dump($items->getProvince_Id());
                    //var_dump($this->Agentmodel->getId());
                    
                    $agentid=$this->Agentmodel->getId();
                    if(empty($agentid)){
                        $agent_name = "Đại lý này đã bị xoá";
                    } else {
                        $agent_name = (GlobalLib::getName('agent', $items->getAgent_Id(), 'name'));
                    }
                }
                $menber[] = array(
                    'id' => $items->getId(),
                    'area_name' => $area_name,
                    'province_name' => $province_name,
                    'district_name' => $district_name,
                    'agent_name' => $agent_name,
                    'product_name' => (GlobalLib::getName('product', $items->getProduct_Id(), 'name')),
                    'factor' => $items->getFactor(),
                    'from_date' => $items->getFrom_Date(),
                    'to_date' => $items->getTo_Date(),
                    'event' => $event
                );
            }
        }
        echo json_encode($menber);
        exit();
    }    
    
    public function deleteAction(){
        $id= $this->_getParam("id","");
        $redirectUrl=$this->aConfig["site"]["url"]."admin/areaagentproduct/index";
        $this->modelMapper->delete($id);
        $this->_redirect($redirectUrl);
    }
}
